package com.shanshi.activity.tool;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;

public class OkHttpTool {
	
	public static final String BASE_URL = "http://192.168.28.248:8080/SsProject/app/";

    public static final com.squareup.okhttp.MediaType JSON= MediaType.parse("application/json; charset=utf-8");

	//get请求同步
	public static String doGet(String url){
        //创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .build();
        return  execute(request);
    }
	
	//get请求异步
	public static void doGet(String url,setOkhttpListener mlistener){
        //创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .build();
        listener=mlistener;
        executes(request,mlistener);
    }
	
	//post请求同步
    public static String doPost(String url,String reqbody){
    	RequestBody body =RequestBody.create(JSON,reqbody);
        Request request = new Request.Builder()
        .url(url)
        .post(body)
        .build();
      return execute(request);
    }
  //post请求异步
    public static void doPost(String url,String reqbody,setOkhttpListener mlistener){
    	RequestBody body =RequestBody.create(JSON, reqbody);
        Request request = new Request.Builder()
        .url(url)
        .post(body)
        .build();
      executes(request,mlistener);
    }
    //put请求同步
    public static String doPut(String url,String reqbody){
         RequestBody body =RequestBody.create(JSON, reqbody);
          Request request = new Request.Builder()
          .url(url)
          .put(body)
          .build();
        return execute(request);
    }
    //put请求异步
    public static void doPut(String url,String reqbody,setOkhttpListener mlistener){
        RequestBody body =RequestBody.create(JSON, reqbody);
        Request request = new Request.Builder()
                .url(url)
                .put(body)
                .build();
        executes(request,mlistener);
    }
    
    //delete请求同步
    public static String doDelete(String url,String reqbody){
    	RequestBody body =RequestBody.create(JSON, reqbody);
        Request request = new Request.Builder()
        .url(url)
        .delete(body)
        .build();
      return execute(request);
    }
    //delete请求异步
    public static void doDelete(String url,String reqbody,setOkhttpListener mlistener){
        RequestBody body =RequestBody.create(JSON,reqbody);
        Request request = new Request.Builder()
                .url(url)
                .delete(body)
                .build();
        executes(request,mlistener);
    }
    

     public static String execute(Request request){
         try {
             OkHttpClient client = new OkHttpClient();
             Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            } 
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
     }
     public static void executes(Request request,final setOkhttpListener listener){
             OkHttpClient client = new OkHttpClient();
             client.newCall(request).enqueue(new Callback(){

				@Override
				public void onFailure(Request arg0, IOException arg1) {
					// TODO Auto-generated method stub
					listener.onFail();

				}

				@Override
				public void onResponse(Response arg0) throws IOException {
					// TODO Auto-generated method stub
					listener.onSuccess(arg0.body().string());
				}

             });
     }
     public static interface setOkhttpListener{
    	 void onSuccess(String s);
    	 void onFail();
     }
     private static setOkhttpListener listener=null;
     
}
